from django.shortcuts import render
from rest_framework.viewsets import ModelViewSet,GenericViewSet
from rest_framework.mixins import CreateModelMixin,ListModelMixin,DestroyModelMixin
from rest_framework.permissions import  IsAuthenticated
from rest_framework.pagination import PageNumberPagination
from rest_framework.response import Response
from rest_framework.status import HTTP_200_OK
from django.db.models import F
# Create your views here.
from community.models import Article
from .serializers import *
from utils.customPermissions import auto_user

class ArticlePageNumberPagination(PageNumberPagination): # 文章的分页类
    page_size = 10 #默认10条文章
    page_size_query_param = 'size'
    max_page_size = 100 # 最大100条

class ArticleViewSet(ModelViewSet):
    queryset = Article.objects.filter(is_delete=False,status=1) # 获取已发布未删除的文章
    serializer_class = ArticleSerializer
    permission_classes = [IsAuthenticated] # 内置登录权限
    pagination_class = ArticlePageNumberPagination

    @auto_user # 不管选择的是谁，自动给你换成当前登录用户
    def create(self, request, *args, **kwargs):
        serializer =self.get_serializer(data=request.data)
        serializer.is_valid(raise_exception=True)
        serializer.save()
        return Response(serializer.data,status=HTTP_200_OK)

    @auto_user  # 不管选择的是谁，自动给你换成当前登录用户
    def update(self, request, *args, **kwargs):
        # 用当前登录用户和当前文章的id去数据库进行搜索，没有找到搜索，则说明当前文章不是当前用户创建的
        if not Article.objects.filter(id=int(kwargs['pk']),user=request.user):
            return Response({'msg':'您没有修改权限'})

        article = Article.objects.get(id=int(kwargs['pk']))
        serializer = self.get_serializer(article,data=request.data)
        serializer.is_valid()
        serializer.save()
        return Response(serializer.data,status=HTTP_200_OK)

    def destroy(self, request, *args, **kwargs):
        # 用当前登录用户和当前文章的id去数据库进行搜索，没有找到搜索，则说明当前文章不是当前用户创建的
        if not Article.objects.filter(id=int(kwargs['pk']), user=request.user):
            return Response({'msg': '您没有删除权限'})

        return ModelViewSet.destroy(self,request,*args,**kwargs)

    def retrieve(self, request, *args, **kwargs):
        # F 查询返回查询的符合条件的数据的指定字段的值
        Article.objects.filter(id=int(kwargs['pk'])).update(page_view=F('page_view')+1)
        return ModelViewSet.retrieve(self,request,*args,**kwargs)

class CommentViewSet(GenericViewSet,CreateModelMixin,ListModelMixin,DestroyModelMixin):
    queryset = Comment.objects.filter(level=1)
    # serializer_class = CommentSerializer
    serializer_class = CommentHandlerSerializer
    permission_classes = [IsAuthenticated]

    # @auto_user  # 不管选择的是谁，自动给你换成当前登录用户
    def create(self, request, *args, **kwargs):
        return CreateModelMixin.create(self, request, *args, **kwargs)

    def destroy(self, request, *args, **kwargs):
        # 用当前登录用户和当前文章的id去数据库进行搜索，没有找到搜索，则说明当前文章不是当前用户创建的
        if not Comment.objects.filter(id=int(kwargs['pk']), user=request.user):
            return Response({'msg': '您没有删除权限'})

        return DestroyModelMixin.destroy(self, request, *args, **kwargs)


